<html>
<link rel="shortcut icon" href="about:blank" />
<style>
	html,
	body {
		height: 100%;
		margin: 0;
		padding: 0;
	}

	h3 {
		text-align: center;
	}

	video {
		display: block;
		margin: 0 auto;
	}

	img {
		display: block;
		margin: 0 auto;
	}

	body {
		display: flex;
		flex-direction: column;
	}

	#content {
		flex: 1;
	}

	#footer {
		text-align: center;
		width: 100%;
	}
</style>

<body>
	<div id="content"></div>
	<footer id="footer"></footer>
	<script src="hls.js"></script>
	<script>
		fetch('version')
			.then(response => response.text())
			.then(version => {
				document.getElementById("footer").innerHTML = "<p><a href='https://github.com/mpromonet/v4l2rtspserver'>v4l2rtspserver</a>"
					+ " " + version + "</p>";
			});

		fetch('streamlist')
			.then(response => response.json())
			.then(streamList => {
				const content = document.getElementById("content")
				streamList.forEach(stream => {
					const [name, formats] = Object.entries(stream)[0];

					const title = document.createElement("h3");
					title.innerText = name;
					content.appendChild(title)
					if (formats.some(fmt => fmt.toUpperCase().includes("JPEG"))) {
						const img = document.createElement("img");
						img.src = "/snapshot?" + name;
						content.appendChild(img)
					}
					else if (formats.some(fmt => fmt.toUpperCase().includes("MP2T"))) {
						const video = document.createElement("video");
						content.appendChild(video)
						const hls = new Hls();
						hls.loadSource(stream + ".m3u8");
						hls.attachMedia(video);
						hls.on(Hls.Events.MANIFEST_PARSED, function () { video.play(); });
					}
				})
			});

	</script>
</body>

</html>